// $Id: Recording.java 365 2009-05-15 08:31:17Z huijzer $

/*    
 Aoide is a web-based music catalog using the Spring Framework.
 Copyright (C) 2009  Arjan Huijzer

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 You can contact Arjan Huijzer by sending an e-mail to huijzer@gmail.com.
 */

package org.aoide.core.persistence;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import nu.xom.Attribute;
import nu.xom.Document;
import nu.xom.Element;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Recording {

	public static final String UUID = "uuid";
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private String uuid;

	@Persistent
	private String title;

	@Persistent
	private String type;

	@Persistent
	private Integer year;

	@Persistent
	private Blob cover;

	@Persistent
	private Date created;

	@Persistent(mappedBy = "recordings")
	private Artist artist;

	@Persistent
	private Text description;

	public Key getKey() {
		return key;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Integer getYear() {
		return year;
	}

	public void setYear(Integer year) {
		this.year = year;
	}

	public Blob getCover() {
		return cover;
	}

	public void setCover(Blob cover) {
		this.cover = cover;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public Artist getArtist() {
		return artist;
	}

	public void setArtist(Artist artist) {
		this.artist = artist;
	}

	public Text getDescription() {
		return description;
	}

	public void setDescription(Text description) {
		this.description = description;
	}

	public Element toXmlElement() {

		// Create <recording> document element
		Element recordingElement = new Element("recording");
		recordingElement.addAttribute(new Attribute("uuid", uuid));

		// Create <title> element
		Element titleElement = new Element("title");
		titleElement.appendChild(title);
		recordingElement.appendChild(titleElement);

		// Create <year> element
		if (year != null) {
			Element yearElement = new Element("year");
			yearElement.appendChild(year.toString());
			recordingElement.appendChild(yearElement);
		}

		// Create <created> element
		Element createdElement = new Element("created");
		createdElement.appendChild(created.toString());
		recordingElement.appendChild(createdElement);

		// Create <type> element
		if (StringUtils.isNotBlank(type)) {
			Element typeElement = new Element("type");
			typeElement.appendChild(type);
			recordingElement.appendChild(typeElement);
		}
		
		// Create <description> element
		if (description != null) {
			Element descriptionElement = new Element("description");
			descriptionElement.appendChild(description.getValue());
			recordingElement.appendChild(descriptionElement);
		}
		
		// Create <cover> element
		if (cover != null) {
			Element coverElement = new Element("cover");
			coverElement.addAttribute(new Attribute("encoding", "base64"));
			coverElement.addAttribute(new Attribute("mimetype", "image/jpeg"));
			byte[] encodedCover = Base64.encodeBase64Chunked(cover.getBytes());
			coverElement.appendChild(new String(encodedCover));
			recordingElement.appendChild(coverElement);
		}

		return recordingElement;
	}

	public Document toXmlDocument() {
		Element recording = this.toXmlElement();
		recording.appendChild(artist.toXmlElement());
		Document doc = new Document(recording);
		return doc;
	}

	public String toString() {
		return new ToStringBuilder(this).append("uuid", uuid).append("title",
				title).toString();
	}

}
